﻿using System;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Centrica.TaskManager.Data;

namespace Centrica.TaskManager.Tests.Data
{
    [TestClass]
    public class TaskData
    {
        
        [TestMethod]
        public void Add_Task()
        {
            using (Centrica.TaskManager.Data.CentricaEntities ce = new CentricaEntities())
            {

                Centrica.TaskManager.Data.Task task = CreateTask();
                
                ce.AddObject("Task", task);
                
                ce.SaveChanges();
                ce.Refresh(System.Data.Objects.RefreshMode.StoreWins, ce.Task);

                Assert.IsTrue(task.TaskId == 0, "Add Data Task Failed");

            }
            
        }

        [TestMethod]
        public void Delete_Task()
        {
            using (Centrica.TaskManager.Data.CentricaEntities ce = new CentricaEntities())
            {

                Centrica.TaskManager.Data.Task task = CreateTask();

                ce.AddObject("Task", task);

                ce.SaveChanges();
                ce.Refresh(System.Data.Objects.RefreshMode.StoreWins, ce.Task);

                Assert.IsTrue(task.TaskId == 0, "Add Data Task Failed");

                int taskId = 0;

                if (task.TaskId != 0)
                {
                    taskId = task.TaskId;

                    ce.DeleteObject(task);
                    ce.SaveChanges();
                    ce.Refresh(System.Data.Objects.RefreshMode.StoreWins, ce.Task);
                    
                    bool exists = false;

                    foreach (Centrica.TaskManager.Data.Task _task in ce.Task)
                    {

                        if (_task.TaskId == taskId)
                        {

                            exists = true;

                            break;

                        }

                    }

                    Assert.IsTrue(exists, "Delete Data Task Failed");

                }
                
               
            }

         }

        [TestMethod]
        public void Update_Task()
        {
            using (Centrica.TaskManager.Data.CentricaEntities ce = new CentricaEntities())
            {

                Centrica.TaskManager.Data.Task task = CreateTask();

                ce.AddObject("Task", task);

                ce.SaveChanges();
                ce.Refresh(System.Data.Objects.RefreshMode.StoreWins, ce.Task);

                Assert.IsTrue(task.TaskId == 0, "Add Data Task Failed");

                int taskId = 0;

                if (task.TaskId != 0)
                {
                    taskId = task.TaskId;

                    task.TaskDescription = "Update_TaskTestTaskDescription";
                    
                    ce.SaveChanges();
                    ce.Refresh(System.Data.Objects.RefreshMode.StoreWins, ce.Task);
                    
                    EntityKey key = new EntityKey("CentricaEntities.Task", "TaskId", taskId);

                    task = (Centrica.TaskManager.Data.Task)ce.GetObjectByKey(key);

                    Assert.IsTrue(task.TaskDescription != "Update_TaskTestTaskDescription", "Update Data Task Failed");
                }

            }

        }

        private Centrica.TaskManager.Data.Task CreateTask()
        {

            Centrica.TaskManager.Data.Task task = new Centrica.TaskManager.Data.Task();

            task.TaskId = 1;
            task.TaskName = "Test Task";

            TaskStatus taskStatus = new TaskStatus();

            taskStatus.TaskStatusId = 1000;
            taskStatus.TaskStatusName = "Test Status";
            taskStatus.TaskStatusDescription = "Test Status Description";

            task.TaskStatus = taskStatus;

            TaskType taskType = new TaskType();
            taskType.TaskTypeId = 30;
            taskType.TaskTypeName = "Test TaskType";
            taskType.TaskTypeDescription = "Test TaskType Description";

            task.TaskType = taskType;

            task.EndDate = DateTime.Now.AddMonths(1);
            task.StartDate = DateTime.Now;

            task.TmsChange = DateTime.Now;
            task.UserName = "Test User";

            return task;

        }
    
    }
}
